using BasicScript.Component;
using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class HeightMachineItem : BaseItem
	{
		private const float MAX_PLOE_Y = 4.5f;

		private const float MIN_PLOE_Y = 0.6f;

		private const int MAX_NUM = 200;

		private float ploeY;

		private float lastTouchY;

		private int num;

		private Transform pole;

		private SpriteRenderer[] nums;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			pole = model.Find("Pole");
			Transform transform = model.Find("Num");
			if (transform != null)
			{
				nums = new SpriteRenderer[3];
				nums[0] = transform.Find("0").GetComponent<SpriteRenderer>();
				nums[1] = transform.Find("1").GetComponent<SpriteRenderer>();
				nums[2] = transform.Find("2").GetComponent<SpriteRenderer>();
			}
			if (data.modelY != 0f)
			{
				SetPoleY(data.modelY);
			}
			else
			{
				SetPoleY(pole.localPosition.y);
			}
		}

		private void SetPoleY(float val)
		{
			if (val > 4.5f)
			{
				val = 4.5f;
			}
			else if (val < 0.6f)
			{
				val = 0.6f;
			}
			ploeY = val;
			pole.SetLocalY(ploeY);
			num = Mathf.FloorToInt((ploeY - 0.6f) / 3.9f * 200f);
			InitNumber(num);
		}

		public void InitNumber(int val)
		{
			if (num < 0)
			{
				num = 0;
			}
			else if (num > 200)
			{
				num = 200;
			}
			if (nums == null)
			{
				return;
			}
			string[] array = new string[3];
			if (val < 10)
			{
				array[0] = "0";
				array[1] = "0";
				array[2] = val.ToString();
			}
			else if (val < 100)
			{
				array[0] = "0";
				array[1] = num.ToString().Substring(0, 1);
				array[2] = num.ToString().Substring(1, 1);
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
				array[2] = num.ToString().Substring(2, 1);
			}
			SpriteAssets component = model.GetComponent<SpriteAssets>();
			for (int i = 0; i < array.Length; i++)
			{
				string value = array[i];
				if (!(component != null) || !(component.data != null) || component.data.sprites == null)
				{
					continue;
				}
				for (int j = 0; j < component.data.sprites.Count; j++)
				{
					if (component.data.sprites[j] != null && component.data.sprites[j].name.Equals(value))
					{
						nums[i].sprite = component.data.sprites[j];
						break;
					}
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			lastTouchY = touchEventData.curPos.y;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			SetPoleY(pole.localPosition.y + touchEventData.curPos.y - lastTouchY);
			lastTouchY = touchEventData.curPos.y;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["39"] = ploeY.ToString("F3");
		}
	}
}
